//
//  WBVideoPlayerViewController.m
//  JPVideoPlayerDemo
//
//  Created by xin on 2018/4/12.
//  Copyright © 2018年 NewPan. All rights reserved.
//

#import "WBVideoPlayerViewController.h"
#import "UIView+WebVideoCache.h"
#import <JPNavigationControllerKit.h>
#import "JPVideoPlayerWBControlView.h"
#import "JPVideoPlayerWBControlBar.h"

@interface WBVideoPlayerViewController () <JPVideoPlayerDelegate>

@property (nonatomic, strong) UIView *videoContainer;
@property (weak, nonatomic) IBOutlet UIButton *voiceButton;

@end

@implementation WBVideoPlayerViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    // 使用 `JPNavigationController` 处理 pop 手势导致 `AVPlayer` 播放器播放视频卡顿.
    self.navigationController.jp_useCustomPopAnimationForCurrentViewController = YES;
//    View controller-based status bar appearance
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(muteButtonChanged:) name:JPVideoPlayerControlTopBarMuteChangedNotification object:nil];
    
    self.videoContainer = ({
        UIView *videoView = [UIView new];
        videoView.backgroundColor = [UIColor blackColor];
        CGFloat screenWid = [UIScreen mainScreen].bounds.size.width;
        videoView.frame = CGRectMake(0, 77, screenWid, screenWid);
        [self.view addSubview:videoView];
        videoView.jp_videoPlayerDelegate = self;
        videoView;
    });
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    JPVideoPlayerWBControlBar *bar = [[JPVideoPlayerWBControlBar alloc] initWithProgressView:nil];
    JPVideoPlayerWBControlView *controlView = [[JPVideoPlayerWBControlView alloc] initWithControlBar:bar blurImage:nil];
    
    [self.videoContainer jp_playVideoWithURL:[NSURL URLWithString:self.videoPath]
                           bufferingIndicator:nil
                                  controlView:controlView
                                 progressView:nil
                      configurationCompletion:^(UIView *view, JPVideoPlayerModel *playerModel) {
//                          self.muteSwitch.on = ![self.videoContainer jp_muted];
                      }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.videoContainer jp_stopPlay];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

#pragma mark - Click Events

- (IBAction)muteSwitch:(UIButton *)voiceButton {
    voiceButton.selected = !voiceButton.selected;
    self.videoContainer.jp_muted = voiceButton.selected;
}

- (IBAction)closeBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - JPVideoPlayerControlTopBar Notification
- (void)muteButtonChanged:(NSNotification *)noti
{
    BOOL mute = [noti.object boolValue];
    self.voiceButton.selected = self.videoContainer.jp_muted;
}

#pragma mark - JPVideoPlayerDelegate

- (BOOL)shouldAutoReplayForURL:(nonnull NSURL *)videoURL {
    return NO;
}

@end
